package com.chl.jdk8.function;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.Consumer;

import org.junit.Test;

public class FunctionDemo {

	// 对象 :: 实例方法
	// Consumer中的void accept(T t)
	// PrintStream中的void println(T t)
	@Test
	public void test1() {
		// 消费型方法
		Consumer<String> con1 = s -> System.out.println(s);
		con1.accept("北京");
		PrintStream ps = new PrintStream(System.out);
		Consumer<String> con2 = ps::println;
		con2.accept("南京");
		Consumer<String> con3 = System.out::println;
		con3.accept("55");
	}

	// 类 :: 静态方法
	// Comparator中的 int compare(T t1,T t2)
	// Integer中的 int compare(T t1,T t2)
	@Test
	public void test2() {
		Comparator<Integer> c1 = (o1, o2) -> Integer.compare(o1, o2);
		System.out.println(c1.compare(12, 23));
		Comparator<Integer> c2 = Integer::compareTo;
		System.out.println(c2.compare(12, 12));

	}

	// 情况三：类 :: 实例方法
	// Comparator中的int comapre(T t1,T t2)
	// String中的int t1.compareTo(t2) t1作为调用者出现
	@Test
	public void test3() {
		Comparator<String> c1 = (s1, s2) -> s1.compareTo(s2);
		System.out.println(c1.compare("abc", "abd"));
		Comparator<String> c2 = String::compareTo;
		System.out.println(c2.compare("abc", "abm"));
	}

}
